package tests;

import common.Utils;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class ArticleListPage extends Utils {
    private static String url = "http://8.134.99.135:58080/index.html";

    public ArticleListPage() {
        super(url);
    }

    //未登录的情况下，是否会自动跳转到登陆页面
    public void noLoginArticle(){
        String currentUrl = driver.getCurrentUrl();
        Assertions.assertEquals("http://8.134.99.135:58080/sign-in.html",currentUrl);
    }

    //登录的情况下，检查页面是否可以正常加载
    public void loginArticle(){
        //log
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md." +
                "navbar-light.d-print-none > div > h1 > a > img"));
        //搜索框
        String searchText = driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none " +
                "> div > div > div.nav-item.d-none.d-md-flex.me-3 > div > form > div > input")).getAttribute("placeholder");
        Assertions.assertEquals("输入关键字...",searchText);
        //白天/黑夜模式切换模块
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none " +
                "> div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-dark > svg"));
        //私信模块
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none " +
                "> div > div > div:nth-child(2) > div > a"));
        //个人信息模块
        driver.findElement(By.cssSelector("#index_nav_avatar"));


        //菜单模块（此处仅检查菜单列表是否正确加载出来）
        //1、首页
        String homeText = driver.findElement(By.cssSelector("#nav_board_index > a > span.nav-link-title")).getText();
        Assertions.assertEquals("首页",homeText);
        //2、Java
        String javaText = driver.findElement(By.cssSelector("#topBoardList > li:nth-child(2) > a > " +
                "span.nav-link-title")).getText();
        Assertions.assertEquals("Java",javaText);
        //C++
        String cText = driver.findElement(By.cssSelector("#topBoardList > li:nth-child(3) > a > " +
                "span.nav-link-title")).getText();
        Assertions.assertEquals("C++",cText);
        //前端技术
        String leadingText = driver.findElement(By.cssSelector("#topBoardList > li:nth-child(4) > a " +
                "> span.nav-link-title")).getText();
        Assertions.assertEquals("前端技术",leadingText);
        //MySQL
        String mysqlText = driver.findElement(By.cssSelector("#topBoardList > li:nth-child(5) > a > " +
                "span.nav-link-title")).getText();
        Assertions.assertEquals("MySQL",mysqlText);
        //面试宝典
        String interviewText = driver.findElement(By.cssSelector("#topBoardList > li:nth-child(6) > a > " +
                "span.nav-link-title")).getText();
        Assertions.assertEquals("面试宝典",interviewText);
        //经验分享
        String experienceText = driver.findElement(By.cssSelector("#topBoardList > li:nth-child(7) > a > " +
                "span.nav-link-title")).getText();
        Assertions.assertEquals("经验分享",experienceText);
        //招聘信息
        String inviteText = driver.findElement(By.cssSelector("#topBoardList > li:nth-child(8) > a > " +
                "span.nav-link-title")).getText();
        Assertions.assertEquals("招聘信息",inviteText);
        //福利待遇
        String welfareText = driver.findElement(By.cssSelector("#topBoardList > li:nth-child(9) > a > " +
                "span.nav-link-title")).getText();
        Assertions.assertEquals("福利待遇",welfareText);
        //灌水区
        String restsText = driver.findElement(By.cssSelector("#topBoardList > li:nth-child(10) > a >" +
                " span.nav-link-title")).getText();
        Assertions.assertEquals("灌水区",restsText);

        //发布帖子按钮
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bit-forum-content > div.page-header." +
                "d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary." +
                "d-none.d-sm-inline-block.article_post")));
    }

    //检查按板块划分的帖子列表是否可以正确加载(已登录)
    public void plateList(){
        //1、首页
        String homeText = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        Assertions.assertEquals("首页",homeText);

        //检查帖子的相关信息是否已经加载
        //整个帖子模块
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div"));
        //帖子名称
        String titleText = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > " +
                "div.col > div.text-truncate > a > strong")).getText();
        Assertions.assertNotNull(titleText);
        //帖子作者名
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col >" +
                " div.text-muted.mt-2 > div > div.col > ul > li:nth-child(1)"));
        //帖子发布时间
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > " +
                "div.text-muted.mt-2 > div > div.col > ul > li:nth-child(2)"));
        //帖子浏览量
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > " +
                "div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(1)"));
        //帖子点赞数
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > " +
                "div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(2)"));
        //帖子回复数
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > " +
                "div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(3)"));


        //2、Java
        //点击Java模块
        driver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[2]")).click();

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#article_list_count_board")));
        //板块名称
        String nameText = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        Assertions.assertEquals("Java", nameText);
        //板块的帖子数量
        String numText = driver.findElement(By.cssSelector("#article_list_count_board")).getText();
        Assertions.assertTrue(numText.contains("帖子数量:"), "内容与预期有误");
        //板块的帖子列表
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div"));
        //发布帖子按钮
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bit-forum-content > div.page-header." +
                "d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary." +
                "d-none.d-sm-inline-block.article_post")));

        //3、C++
        //点击C++模块
        driver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[3]")).click();

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#article_list_count_board")));
        //板块名称
        nameText = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        Assertions.assertEquals("C++", nameText);
        //板块的帖子数量
        numText = driver.findElement(By.cssSelector("#article_list_count_board")).getText();
        Assertions.assertTrue(numText.contains("帖子数量:"), "内容与预期有误");
        //板块的帖子列表
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div"));
        //发布帖子按钮
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bit-forum-content > div.page-header." +
                "d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary." +
                "d-none.d-sm-inline-block.article_post")));

        //4、前端技术
        //点击前端技术模块
        driver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[4]")).click();

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#article_list_count_board")));
        //板块名称
        nameText = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        Assertions.assertEquals("前端技术", nameText);
        //板块的帖子数量
        numText = driver.findElement(By.cssSelector("#article_list_count_board")).getText();
        Assertions.assertTrue(numText.contains("帖子数量:"), "内容与预期有误");
        //板块的帖子列表
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div"));
        //发布帖子按钮
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bit-forum-content > div.page-header." +
                "d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary." +
                "d-none.d-sm-inline-block.article_post")));

        //5、MySQL
        //点击MySQL模块
        driver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[5]")).click();

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#article_list_count_board")));
        //板块名称
        nameText = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        Assertions.assertEquals("MySQL", nameText);
        //板块的帖子数量
        numText = driver.findElement(By.cssSelector("#article_list_count_board")).getText();
        Assertions.assertTrue(numText.contains("帖子数量:"), "内容与预期有误");
        //板块的帖子列表
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div"));
        //发布帖子按钮
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bit-forum-content > div.page-header." +
                "d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary." +
                "d-none.d-sm-inline-block.article_post")));


        //6、面试宝典
        //点击面试宝典模块
        driver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[6]")).click();

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#article_list_count_board")));
        //板块名称
        nameText = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        Assertions.assertEquals("面试宝典", nameText);
        //板块的帖子数量
        numText = driver.findElement(By.cssSelector("#article_list_count_board")).getText();
        Assertions.assertTrue(numText.contains("帖子数量:"), "内容与预期有误");
        //板块的帖子列表
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div"));
        //发布帖子按钮
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bit-forum-content > div.page-header." +
                "d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary." +
                "d-none.d-sm-inline-block.article_post")));


        //7、经验分享
        //点击经验分享模块
        driver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[7]")).click();

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#article_list_count_board")));
        //板块名称
        nameText = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        Assertions.assertEquals("经验分享", nameText);
        //板块的帖子数量
        numText = driver.findElement(By.cssSelector("#article_list_count_board")).getText();
        Assertions.assertTrue(numText.contains("帖子数量:"), "内容与预期有误");
        //板块的帖子列表
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div"));
        //发布帖子按钮
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bit-forum-content > div.page-header." +
                "d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary." +
                "d-none.d-sm-inline-block.article_post")));


        //8、招聘信息
        //点击招聘信息模块
        driver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[8]")).click();

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#article_list_count_board")));
        //板块名称
        nameText = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        Assertions.assertEquals("招聘信息", nameText);
        //板块的帖子数量
        numText = driver.findElement(By.cssSelector("#article_list_count_board")).getText();
        Assertions.assertTrue(numText.contains("帖子数量:"), "内容与预期有误");
        //板块的帖子列表
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div"));
        //发布帖子按钮
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bit-forum-content > div.page-header." +
                "d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary." +
                "d-none.d-sm-inline-block.article_post")));


        //9、福利待遇
        //点击福利待遇模块
        driver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[9]")).click();

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#article_list_count_board")));
        //板块名称
        nameText = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        Assertions.assertEquals("福利待遇", nameText);
        //板块的帖子数量
        numText = driver.findElement(By.cssSelector("#article_list_count_board")).getText();
        Assertions.assertTrue(numText.contains("帖子数量:"), "内容与预期有误");
        //板块的帖子列表
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div"));
        //发布帖子按钮
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bit-forum-content > div.page-header." +
                "d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary." +
                "d-none.d-sm-inline-block.article_post")));


        //10、灌水区
        //点击灌水区模块
        driver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[10]")).click();

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#article_list_count_board")));
        //板块名称
        nameText = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        Assertions.assertEquals("灌水区", nameText);
        //板块的帖子数量
        numText = driver.findElement(By.cssSelector("#article_list_count_board")).getText();
        Assertions.assertTrue(numText.contains("帖子数量:"), "内容与预期有误");
        //板块的帖子列表
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div"));
        //发布帖子按钮
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bit-forum-content > div.page-header." +
                "d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary." +
                "d-none.d-sm-inline-block.article_post")));
    }


}
